<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 15-5-4
 * Time: 下午3:50
 */

class label extends adminBase{
    function __construct() {
        parent::__construct();
        $this->cms->setPageTitle("标签管理");
        $admin_info=$this->user->getLoginInfo();
        $user=UserMode::init();
        $levels=$user->getLevels($admin_info['id']);
        if(!$user->checkLevels(8,$levels)){
            $r['message']="您不具有此权限！";
            View::displayAsHtml($r,"plugin/state/tips.php");
            exit();
        }

    }

    function indexTask(){
        $url=WebRouter::init()->getQuestion("page=");
        $page=isset($_GET['page'])&&($_GET['page']>0)? (int)$_GET['page']:1;
        $offset=((int)$page==1)? '0':($page-1)*20;
        $label=LabelModule::init();
        list($r['label'],$label_total)=$label->getList(300,$offset);
        $r['pages']=new Page($page,$label_total);
        $r['pages']->setPageCallback(create_function('$page','return "'.$url.'".$page;'));
        $this->cms->tableScene($r,"admin/tpl/label/label.php");
    }

    function labelAddTask(){
        $this->cms->formScene(array(),"admin/tpl/label/labelAdd.php");
    }

    function labelAddSubmitTask($name,$show_in_box,$show_in_detail){
        $db=SqlDB::init();
        $label=LabelModule::init();
        $data['show_in_box']=isset($show_in_box)?1:0;
        $data['show_in_detail']=isset($show_in_detail)?1:0;
        $label_id=$label->add($name);
        if($db->update("goods_label",$label_id,$data)){
            $r['url']=WebRouter::init()->getAction("index");
            $r['message']="标签添加成功";
        }else{
            $r['url']=WebRouter::init()->getAction("labelAdd");
            $r['message']="标签添加失败";
        }
        View::displayAsHtml($r,"plugin/state/tips.php");
    }

    function labelModifyTask($id){
        $label=LabelModule::init();
        $r=$label->getDetail($id);
        $this->cms->formScene($r,"admin/tpl/label/labelModify.php");
    }

    function labelModifySubmitTask($id,$name,$show_in_box,$show_in_detail){
        $db=SqlDB::init();
        $data['name']=isset($name)?trim($name):null;
        $data['show_in_box']=isset($show_in_box)?1:0;
        $data['show_in_detail']=isset($show_in_detail)?1:0;
        if($db->update("goods_label",$id,$data)){
            $r['url']=WebRouter::init()->getAction("index");
            $r['message']="标签修改成功";
        }else{
            $r['url']=WebRouter::init()->getAction("index");
            $r['message']="标签未被修改";
        }
        View::displayAsHtml($r,"plugin/state/tips.php");
    }

    function labelDeleteTask($id){
        $label=LabelModule::init();
        if($label->delete($id)){
            $r['url']=WebRouter::init()->getAction("index");
            $r['message']="标签删除成功";
        }else{
            $r['message']="标签删除失败，请重试";
        }
        View::displayAsHtml($r,"plugin/state/tips.php");
    }

} 